<?php

function getuids() {
    return array(1,3,7);
}

function getmgsarr() {
    return  array(
        0 => array(
            1 => array('1-a', '1-b', '1-c', '1-d', '1-e',
                    '1-f', '1-g', '1-h', '1-i', '1-j','1-k',
                    '1-l', '1-m', '1-n', '1-o', '1-p', '1-q',
                    '1-r', '1-s', '1-t'),
            3 => array('3-a', '3-b', '3-c', '3-d', '3-e',
                    '3-f', '3-g', '3-h', '3-i', '3-j','3-k',
                    '3-l', '3-m', '3-n', '3-o', '3-p', '3-q',
                    '3-r', '3-s', '3-t'),
            7 => array('7-a', '7-b', '7-c', '7-d', '7-e',
                    '7-f', '7-g', '7-h', '7-i', '7-j','7-k',
                    '7-l', '7-m', '7-n', '7-o', '7-p', '7-q',
                    '7-r', '7-s', '7-t'),
        ),
        1 => array(
            1 => array('product-1-a', 'product-1-b', 'product-1-c', 'product-1-d', 'product-1-e',
                    'product-1-f', 'product-1-g', 'product-1-h', 'product-1-i', 'product-1-j','product-1-k',
                    'product-1-l', 'product-1-m', 'product-1-n', 'product-1-o', 'product-1-p', 'product-1-q',
                    'product-1-r', 'product-1-s', 'product-1-t'),
            3 => array('product-3-a', 'product-3-b', 'product-3-c', 'product-3-d', 'product-3-e',
                    'product-3-f', 'product-3-g', 'product-3-h', 'product-3-i', 'product-3-j','product-3-k',
                    'product-3-l', 'product-3-m', 'product-3-n', 'product-3-o', 'product-3-p', 'product-3-q',
                    'product-3-r', 'product-3-s', 'product-3-t'),
            7 => array('product-7-a', 'product-7-b', 'product-7-c', 'product-7-d', 'product-7-e',
                    'product-7-f', 'product-7-g', 'product-7-h', 'product-7-i', 'product-7-j','product-7-k',
                    'product-7-l', 'product-7-m', 'product-7-n', 'product-7-o', 'product-7-p', 'product-7-q',
                    'product-7-r', 'product-7-s', 'product-7-t'),
        ),
        3 => array(
            1 => array('shopinfo-1-a', 'shopinfo-1-b', 'shopinfo-1-c', 'shopinfo-1-d', 'shopinfo-1-e',
                    'shopinfo-1-f', 'shopinfo-1-g', 'shopinfo-1-h', 'shopinfo-1-i', 'shopinfo-1-j','shopinfo-1-k',
                    'shopinfo-1-l', 'shopinfo-1-m', 'shopinfo-1-n', 'shopinfo-1-o', 'shopinfo-1-p', 'shopinfo-1-q',
                    'shopinfo-1-r', 'shopinfo-1-s', 'shopinfo-1-t'),
            3 => array('shopinfo-3-a', 'shopinfo-3-b', 'shopinfo-3-c', 'shopinfo-3-d', 'shopinfo-3-e',
                    'shopinfo-3-f', 'shopinfo-3-g', 'shopinfo-3-h', 'shopinfo-3-i', 'shopinfo-3-j','shopinfo-3-k',
                    'shopinfo-3-l', 'shopinfo-3-m', 'shopinfo-3-n', 'shopinfo-3-o', 'shopinfo-3-p', 'shopinfo-3-q',
                    'shopinfo-3-r', 'shopinfo-3-s', 'shopinfo-3-t'),
            7 => array('shopinfo-7-a', 'shopinfo-7-b', 'shopinfo-7-c', 'shopinfo-7-d', 'shopinfo-7-e',
                    'shopinfo-7-f', 'shopinfo-7-g', 'shopinfo-7-h', 'shopinfo-7-i', 'shopinfo-7-j','shopinfo-7-k',
                    'shopinfo-7-l', 'shopinfo-7-m', 'shopinfo-7-n', 'shopinfo-7-o', 'shopinfo-7-p', 'shopinfo-7-q',
                    'shopinfo-7-r', 'shopinfo-7-s', 'shopinfo-7-t'),
        )
    );
}

class WoguToolsAction extends CommonAction
{
    public function comment() {
        if(isset($_POST['docomment'])) {
            $catetype = intval($_POST['catetype']);
            $msgsarr = getmgsarr();
            $msgs = $msgsarr[$catetype];
            $uids = getuids();
            
            for($i = 0; $i < 1; $i++) {
                $uid = $uids[array_rand($uids)];
                $msg = $msgs[$uid][array_rand($msgs[$uid])];
                if($catetype == 0) {
                    $this->artcomment($uid, $msg);
                }
            }
        }
        
        $this->display();
    }

    private function artcomment($uid, $msg) {
        require_once fimport('class/MySql');
        require_once fimport('class/db');

        $share_id = intval($post['share_id']);
        $share = FS('Article')->getShareById($share_id);
        $data = array();
        $data['content'] =  htmlspecialchars(trim($post['content']));
        $data['uid'] = $uid;
        $data['parent_id'] = intval($post['parent_id']);
        $data['share_id'] = $share_id;
        $data['create_time'] = TIME_UTC;
        $comment_id = FDB::insert('article_comment',$data,true);

        //分享评论数量加1
        FDB::query('UPDATE '.FDB::table('article').'
            SET comment_count = comment_count + 1
            WHERE id = '.$share_id);
            
        //at
        if(preg_match_all('/\/\/@([^:]+)\:/i', $data['content'], $matches)) {
            $users = FS('User')->getUsersByName($matches[1]);
            foreach($users as $user) {
                //添加评论消息提示
                $result = FDB::query("INSERT INTO ".FDB::table('user_notice')."(uid, type, num, create_time) VALUES('$user[uid]',4,1,'".TIME_UTC."')", 'SILENT');
                if(!$result)
                    FDB::query("UPDATE ".FDB::table('user_notice')." SET num = num + 1, create_time='".TIME_UTC."' WHERE uid='$user[uid]' AND type=4");
                    
                //添加评论
                FDB::query("INSERT INTO ".FDB::table('atme')."(uid, user_name, share_id, cate_type) VALUES('$user[uid]','$user[user_name]',$comment_id,0)", 'SILENT');
            }
        }
    }
}

function fimport($file_name, $folder = '')
{
    global $_FANWE;
    static $sufix = array(
        'module'=>'.module',
        'service'=>'.service',
        'class'=>'.class',
        'function' => '.func',
        'include' => '.inc',
        'language' => '.lang',
        'cache' => '.cache',
        'dynamic'=>'.dynamic',
    );

    $file_name = strtolower($file_name);
    $file_path = FANWE_ROOT.'./core';
    if(strstr($file_name, '/'))
    {
        list($pre, $name) = explode('/', $file_name);

        $insert = '';
        if($pre == 'language')
            $insert = $_FANWE['config']['default_lang'].'/';

        return "{$file_path}/{$pre}/".$insert.(empty($folder) ? "" : $folder . "/")."{$name}".$sufix[$pre].".php";
    }
    else
    {
        return "{$file_path}/".(empty($folder) ? "" : $folder . "/")."{$file_name}.php";
    }
}

function FS($service)
{
    static $services = array();
    if($services[$service] === NULL)
    {
        require_once fimport("service/".strtolower($service));
        $s = ucfirst($service)."Service";
        $services[$service] = new $s();
        unset($s);
    }

    return $services[$service];
}









